﻿/*
#include<vector>
#include <iostream>
using namespace std;
void test_vector_1()
{
    vector<int> v;
    v.push_back(1);          //在末尾插一个元素1
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    v.push_back(5);

    //遍历vector的三种方式：v[下标]、迭代器、范围for
    //v[下标]
    for (size_t i = 0; i < v.size(); i++)
    {
        cout << v[i] << " ";
    }
    cout << endl;
    //迭代器
    vector<int>::iterator it_v = v.begin();
    while (it_v != v.end())
    {
        cout << *it_v << " ";
        ++it_v;
    }
    cout << endl;
    //范围for
    for (auto e : v)
    {
        cout << e << " ";
    }
    cout << endl;
}

void test_vector_2()
{
    vector<int> v(10, 1);
    //遍历vector的三种方式：v[下标]、迭代器、范围for
    //v[下标]
    for (size_t i = 0; i < v.size(); i++)
    {
        cout << v[i] << " ";
    }
    cout << endl;
    //迭代器
    vector<int>::iterator it_v = v.begin();
    while (it_v != v.end())
    {
        cout << *it_v << " ";
        ++it_v;
    }
    cout << endl;
    //范围for
    for (auto e : v)
    {
        cout << e << " ";
    }
    cout << endl;
}

int main()
{
    test_vector_1();
    test_vector_2();
    return 0;
}
*/

#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;
//vector的扩容机制
void test_vector_1()
{
    size_t sz;                                                        //创建变量sz来存放容量大小
    vector<int> v;                                                    //
    sz = v.capacity();                                                //初始值为0
    cout << "making vector grow:" << endl;
    for (size_t i = 0; i < 100; i++)                                
    { 
        v.push_back(i);                                               //刚进入循环时，sz=0,v.capacity()=1               
        if (sz != v.capacity())
        {
            sz = v.capacity();
            cout << "capacity change :" << sz << endl;
        }
    }
    cout << endl;
}

//开10个空间并且初始化
void test_vector_2()
{
    cout << "开10个空间并且初始化为1" << endl;
    vector<int> a;
    a.resize(10, 1);
    for (auto e : a)
    {
        cout << e << " ";
    }
    cout << endl;
}

//迭代器Iterators:
void test_vector_3()
{
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    //正向遍历
    vector<int>::iterator it_v = v.begin();    
    while (it_v!=v.end())
    {
        cout << *it_v << " ";
        ++it_v;
    }
    cout << endl;
    //逆向遍历
    vector<int>::reverse_iterator it_vs = v.rbegin();
    while (it_vs != v.rend())
    {
        cout << *it_vs << " ";
        ++it_vs;
    }
    cout << endl<<endl;
}

//Capacity: size、max_size、resize、capacity、empty、reserve、shrink_to_fit 
void test_vector_4()
{
    vector<int> v(10, 1);
    cout << " v.max_size() = " << v.max_size() << endl; 
    cout << "-------------------------" << endl;
    cout <<"v.size() = " << v.size() << endl;                    
    cout << "v.capacity() = " << v.capacity() << endl;
    v.resize(100);                                                    //调整容器大小，使其包含 n 个元素。
    cout << "-------------------------" << endl;
    cout << "v.size() = " << v.size() << endl;                   
    cout << "v.capacity() = " << v.capacity() << endl;
    v.reserve(10);                                                    //申请更改容量
    cout << "-------------------------" << endl;
    cout << "v.size() = " << v.size() << endl;                   
    cout << "v.capacity() = " << v.capacity() << endl;
    v.shrink_to_fit();                                                //缩小容量
    cout << "-------------------------" << endl;
    cout << "v.size() = " << v.size() << endl;                   
    cout << "v.capacity() = " << v.capacity() << endl;
    cout << "-------------------------" << endl;

    for (auto e : v)
    {
        cout << e << " ";
    }
    cout << endl << endl;
}

//调用算法的find函数，需要加头文件<algorithm>
void test_vector_5()
{
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    v.push_back(5);
    v.push_back(6);
    //范围for遍历数组v
    for (auto e : v)
    {
        cout << e << " ";
    }
    cout << endl;
    vector<int>::iterator v_pos =find(v.begin(),v.end(),3);      //在迭代器范围v.begin(),v.end()里面查找3
    //上述表达式的另外一种写法
    //auto pos = find(v.begin(), v.end(), 3);
    if (v_pos != v.end())
    {
        v.insert(v_pos, 30);                                     //在v_pos位置前插入数值30
    }
    for (auto e : v)
    {
        cout << e << " ";
    }
    cout << endl;
    //头部插入数值0
    v.insert(v.begin(), 0);                                     //头插
    for (auto e : v)
    {
        cout << e << " ";
    }
    cout << endl;
    //在第三个位置插入数值66
    v.insert(v.begin() + 2, 66);                                //在第3个元素前
    for (auto e : v)
    {
        cout << e << " ";
    }
    cout << endl;
    //头部插入string字符串
    string s("abcd");
    v.insert(v.begin(), s.begin(), s.end());                   //从vector头部插入s.begin(), s.end()范围的字符
    for (auto e : v)
    {
        cout << e << " ";
    }
    cout << endl;
}

void test_vector_6()
{
    vector<string> v;
    //对象数组v
    string st1("苹果");
    v.push_back(st1);                       //插入对象st1
    v.push_back(string("香蕉"));            //匿名对象
    v.push_back("草莓");
    for (auto e : v)
    {
        cout << e << " ";
    }
    cout << endl;
}
int main()
{
    test_vector_1();
    test_vector_2();
    test_vector_3();
    test_vector_4();
    test_vector_5();
    test_vector_6();
}